#!/bin/bash

# Name all the files that you need to check.
SRC="\
main.fp.c \
main.fp83.c \
main.lizard.c \
main.lizard.2.c \
main.base.c \
quadpod.h \
quadpod.fp.h \
Makefile.base \
Makefile.fp \
Makefile.fp83 \
USART/txstr.c \
USART/usart.c \
USART/usart.h \
USART/tx.c \
USART/rx.c \
fixmath/fix16.c \
fixmath/fix16_sqrt.c \
fixmath/fix16_trig.c \
fixmath/fix16.h \
fixmath/fixmath.h \
fixmath/fract32.h \
fixmath/int64.h \
fixmath/uint32.h \
usbdrv/asmcommon.inc \
usbdrv/Changelog.txt \
usbdrv/CommercialLicense.txt \
usbdrv/License.txt \
usbdrv/oddebug.c \
usbdrv/oddebug.h \
usbdrv/Readme.txt \
usbdrv/usbconfig-prototype.h \
usbdrv/usbdrvasm128.inc \
usbdrv/usbdrvasm12.inc \
usbdrv/usbdrvasm15.inc \
usbdrv/usbdrvasm165.inc \
usbdrv/usbdrvasm16.inc \
usbdrv/usbdrvasm18-crc.inc \
usbdrv/usbdrvasm20.inc \
usbdrv/usbdrvasm.asm \
usbdrv/usbdrvasm.S \
usbdrv/usbdrv.c \
usbdrv/usbdrv.h \
usbdrv/USBID-License.txt \
usbdrv/usbportability.h "

echo "** Create Quadpod Firmware **"

if [ -n "$1" ]
then
	echo -n "Detecting configuration ... "
else 
	echo "failed"
	echo "ERROR: no configuration selected. Use 'lizard', 'sway', 'fp75', 'fp83', 'check', 'base', 'translink', 'program'. Bailing out."
	exit -1
fi

if  echo "$1" | grep -q "check" 
then
	configuration="check"
fi

if  echo "$1" | grep -q "lizard" 
then
	cp -f Makefile.lizard Makefile
	configuration="lizard"
fi

if  echo "$1" | grep -q "sway" 
then
	cp -f Makefile.lizard.2 Makefile
	configuration="sway"
fi

if  echo "$1" | grep -q "fp83" 
then
	cp -f Makefile.fp83 Makefile
	configuration="fp83"
fi

if  echo "$1" | grep -q "fp75" 
then
	cp -f Makefile.fp Makefile
	configuration="fp75"
fi

if  echo "$1" | grep -q "base" 
then
	cp -f Makefile.base Makefile
	configuration="base"
fi

if  echo "$1" | grep -q "translink" 
then
	cp -f Makefile.translink Makefile
	configuration="translink"
fi

if echo "$1" | grep -q "program"
then
	configuration="program"
fi

if [ -n "$configuration" ]
then
	echo " OK"
	echo "Looks good, using configuration: $configuration"
else
	echo " failed"
	echo "ERROR: unknown configuration selected. Use 'lizard', 'sway', 'fp75', 'fp83', 'check', 'base', 'translink', 'program'. Bailing out."
	exit -1
fi

#
# List all the files here ...
#
for file in $SRC
do
	echo -n "checking $file ... "
	if [ -f $file ]
	then
		echo " OK"
	else
		echo " failed"
		echo "Perform svn checkout command first"
		exit -1
	fi
done

if echo "$configuration" | grep -q "check" 
then
	echo "** STOP here: all OK **"
	exit -1
fi

if echo "$configuration" | grep -q "program"
then
	if [ -f "main.eep.hex" ]
	then
		if [ -f "main.hex" ]
		then
			echo "INFO: $configuration routine for main.eep.hex ... OK"
			configuration="make program"
			$configuration 
			exit 0
		else
			echo "ERROR: main.hex not found, check the build process"
			exit -1;
		fi	
	else
		echo "ERROR: main.eep.hex not found, check the build process"
		exit -1;
	fi
fi

if echo "$configuration" | grep -q "translink"
then
	if [ -f "Makefile" ]
	then
		ANS=`diff -q Makefile.translink Makefile`
		if echo "$ANS" | grep -q "differ"
		then
			echo "Error, the makefile is wrong. Cannot clobber the original Makefile. Delete it manually and re-run this."
			exit -1;
		fi
	else
		echo "Error, the makefile does not exist. Check write permissions and re-run this."
		exit -1;
	fi
fi

if echo "$configuration" | grep -q "fp75"
then
	if [ -f "Makefile" ]
	then
		ANS=`diff -q Makefile.fp Makefile`
		if echo "$ANS" | grep -q "differ"
		then
			echo "Error, the makefile is wrong. Cannot clobber the original Makefile. Delete it manually and re-run this."
			exit -1;
		fi
	else
		echo "Error, the makefile does not exist. Check write permissions and re-run this."
		exit -1;
	fi
fi

if echo "$configuration" | grep -q "fp83"
then
	if [ -f "Makefile" ]
	then
		ANS=`diff -q Makefile.fp83 Makefile`
		if echo "$ANS" | grep -q "differ"
		then
			echo "Error, the makefile is wrong. Cannot clobber the original Makefile. Delete it manually and re-run this."
			exit -1;
		fi
	else
		echo "Error, the makefile does not exist. Check write permissions and re-run this."
		exit -1;
	fi
fi

if echo "$configuration" | grep -q "lizard"
then
	if [ -f "Makefile" ]
	then
		ANS=`diff -q Makefile.lizard Makefile`
		if echo "$ANS" | grep -q "differ"
		then
			echo "Error, the makefile is wrong. Cannot clobber the original Makefile. Delete it manually and re-run this."
			exit -1;
		fi
	else
		echo "Error, the makefile does not exist. Check write permissions and re-run this."
		exit -1;
	fi
fi

if echo "$configuration" | grep -q "sway"
then
	if [ -f "Makefile" ]
	then
		ANS=`diff -q Makefile.lizard.2 Makefile`
		if echo "$ANS" | grep -q "differ"
		then
			echo "Error, the makefile is wrong. Cannot clobber the original Makefile. Delete it manually and re-run this."
			exit -1;
		fi
	else
		echo "Error, the makefile does not exist. Check write permissions and re-run this."
		exit -1;
	fi
fi
 
if echo "$configuration" | grep -q "base"
then
	if [ -f "Makefile" ]
	then
		ANS=`diff -q Makefile.base Makefile`
		if echo "$ANS" | grep -q "differ"
		then
			echo "Error, the makefile is wrong. Cannot clobber the original Makefile. Delete it manually and re-run this."
			exit -1;
		fi
	else
		echo "Error, the makefile does not exist. Check write permissions and re-run this."
		exit -1;
	fi
fi


echo "Makefile for configuration $configuration has been created. Use command"
echo " "
echo "   make [debug|release|program]"
echo " "
echo "to build and solution and program the processor with the hexapod firmware."
echo "** configuration finished OK **"
